package cn.wolfcode.controller;



import cn.wolfcode.domain.Emp;
import cn.wolfcode.domain.PageResult;
import cn.wolfcode.domain.Result;
import cn.wolfcode.service.EmpService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;

    //分页查询列表
    @GetMapping
    public Result list(String name, Integer gender,
                     @DateTimeFormat(pattern = "yyyy-MM-dd") Date begin,
                      @DateTimeFormat(pattern = "yyyy-MM-dd") Date end,
                     @RequestParam(defaultValue = "1")  Integer page,
                      @RequestParam(defaultValue = "10") Integer pageSize){
        PageResult pageResult = empService.list(name, gender, begin, end, page, pageSize);
        return Result.success(pageResult);
    }
    //根据ID查询 部门
    @GetMapping("/{id}")
    public Result get(@PathVariable Long id){
        Emp emp = empService.getById(id);
        return Result.success(emp);
    }
    //新增部门
    @PostMapping
    public Result add(@RequestBody Emp emp){
      empService.add(emp);
      return Result.success();
   }
   //修改部门
    @PutMapping
    public Result update(@RequestBody Emp emp){
        empService.update(emp);
        return Result.success();
    }
    //批量删除部门
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Long> ids){
        empService.delete(ids);
        return Result.success();
    }

}
